package com.wsx.ones.boots.grpc.register;

import com.wsx.ones.boots.grpc.GrpcConfig;
import com.wsx.ones.boots.grpc.plugin.OnesNameResolverProvider;
import com.wsx.ones.proto.WebGrpc;
import com.wsx.ones.proto.WebService;
import io.grpc.ManagedChannel;
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
import org.junit.Before;
import org.junit.Test;

/**
 * @author wangshuaixin
 * @Title: com.wsx.ones.boots.grpc.register.GrpcClient
 * @Description: TODO
 * @date 2019/05/22
 */
public class GrpcClient {


    private ManagedChannel managedChannel;


    private WebGrpc.WebBlockingStub webBlockingStub;


    private GrpcConfig grpcConfig;

    @Before
    public void before() {
        ///String name = WebGrpc.getServiceDescriptor().getName();
        ///managedChannel = ManagedChannelBuilder.forAddress("127.0.0.1", 8871).usePlaintext().build();
        ///managedChannel = NettyChannelBuilder.forAddress("127.0.0.1", 10811).usePlaintext().build();
        managedChannel = NettyChannelBuilder.forTarget("consul://192.168.40.19:8500")
                        //TODO
                        ///.nameResolverFactory(new OnesNameResolverProvider())
                        .defaultLoadBalancingPolicy("round_robin")
                        .build();
        webBlockingStub = WebGrpc.newBlockingStub(managedChannel);
    }

    @Test
    public void client() {
        simple();
    }

    private void simple() {
        WebService.Request request = WebService.Request.newBuilder().setId("hello").setContent("今天").build();

        WebService.Response response = webBlockingStub.service(request);

        System.out.println(response.getCode() + "==" + response.getMsg());
    }
}
